import axios from 'axios'

// 添加评价
export function addReview(data) {
  return axios.post('/api/reviews', data)
}

// 获取评价详情
export function getReview(id) {
  return axios.get(`/api/reviews/${id}`)
}

// 更新评价
export function updateReview(id, data) {
  return axios.put(`/api/reviews/${id}`, data)
}

// 删除评价
export function deleteReview(id, userId) {
  return axios.delete(`/api/reviews/${id}?userId=${userId}`)
}

// 根据订单ID获取评价
export function getReviewByOrder(orderId) {
  return axios.get(`/api/reviews/order/${orderId}`)
}

// 根据用户ID获取评价列表
export function getUserReviews(userId, params) {
  return axios.get(`/api/reviews/user/${userId}`, { params })
}

// 根据商家ID获取评价列表
export function getMerchantReviews(merchantId, params) {
  return axios.get(`/api/reviews/merchant/${merchantId}`, { params })
}

// 商家回复评价
export function replyReview(id, reply, merchantId) {
  return axios.post(`/api/reviews/${id}/reply?reply=${encodeURIComponent(reply)}&merchantId=${merchantId}`)
}

// 获取商家平均评分
export function getMerchantAverageRating(merchantId) {
  return axios.get(`/api/reviews/merchant/${merchantId}/average-rating`)
}

// 获取商家评价数量
export function getMerchantReviewCount(merchantId, rating) {
  return axios.get(`/api/reviews/merchant/${merchantId}/count${rating ? `?rating=${rating}` : ''}`)
}

// 检查订单是否已评价
export function checkOrderReviewed(orderId) {
  return axios.get(`/api/reviews/order/${orderId}/exists`)
}

// 获取最新评价列表
export function getLatestReviews(limit, merchantId) {
  let url = '/api/reviews/latest'
  if (limit || merchantId) {
    url += '?'
    if (limit) url += `limit=${limit}`
    if (limit && merchantId) url += '&'
    if (merchantId) url += `merchantId=${merchantId}`
  }
  return axios.get(url)
}

// 搜索评价
export function searchReviews(params) {
  return axios.get('/api/reviews/search', { params })
}

// 获取评价统计信息
export function getReviewStats(merchantId) {
  return axios.get(`/api/reviews/stats/merchant/${merchantId}`)
}

// 获取商家评价统计信息
export function getReviewStatistics(merchantId) {
  return axios.get(`/api/reviews/statistics/${merchantId}`)
}

// 获取有图评价列表
export function getReviewsWithImages(merchantId, params) {
  return axios.get(`/api/reviews/with-images/merchant/${merchantId}`, { params })
}

// 获取用户是否可以对订单进行评价
export function canReviewOrder(orderId, userId) {
  return axios.get(`/api/reviews/can-review/order/${orderId}/user/${userId}`)
} 